﻿using System;
using System.Web.UI;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

public partial class Camara_candidato_Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnEntrar_Click(object sender, ImageClickEventArgs e)
    {
        if (ValidaCPF(txtCPF.Text, txtSenha.Text))
        {
            Session["cpf"] = txtCPF.Text;
            Response.Redirect("saudacao.aspx");
        }
        else
            Response.Write("<script>alert('Candidato não encontrado. Verifique se você já fez o primeiro acesso.');history.go(-1);</script>");

    }

    protected bool ValidaCPF(string cpf, string senha)
    {
        bool valida = false;
        SqlConnection conexaoIntranet = new SqlConnection(ConfigurationManager.ConnectionStrings["conexao_intranet"].ConnectionString);
        SqlCommand consultaLogin = null;
        SqlDataReader drLogin;
        try
        {
            if (conexaoIntranet != null && conexaoIntranet.State != ConnectionState.Open)
                conexaoIntranet.Open();
            string instrucao = "Select cpf, nomealuno, email, disponivel FROM CamaraInscritos where CPF = '" + cpf + "' and SENHA = '" + senha + "'";
            consultaLogin = new SqlCommand(instrucao, conexaoIntranet);
            drLogin = consultaLogin.ExecuteReader();
            if (drLogin.HasRows)
            {
                drLogin.Read();
                Session["nome"] = drLogin["NomeAluno"].ToString();
                Session["email"] = drLogin["Email"].ToString();
                Session["disponivel"] = drLogin["disponivel"].ToString();
                valida = true;
            }
            drLogin.Close();
        }
        finally
        {
            conexaoIntranet.Close();
        }
        return valida;
    }
}